<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="100px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="12" :md="12">
            <el-form-item label="支付订单号:">
              <el-input
                v-model="where.pay_order_number"
                placeholder="请输入支付订单号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="12">
            <el-form-item label="状态:">
              <el-select clearable v-model="where.status" class="ele-fluid">
                <el-option label="未请求" value="1" />
                <el-option label="处理中" value="2" />
                <el-option label="处理完成" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="12">
            <el-form-item label="支付时间:">
              <el-date-picker
                style="width: 100%"
                v-model="where.add_time"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                label-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
              <el-button @click="exportExcel"
                >导出
                <a
                  id="Export"
                  :href="
                    process +
                      `/Ordersub/export?pay_order_number=${
                        this.where.pay_order_number ? this.where.pay_order_number : ''
                      }&status=${
                        this.where.status ? this.where.status : ''
                      }&add_time=${
                        this.where.add_time ? this.where.add_time : ''
                      }&authinfo=${this.urlEncodedToken}`
                  "
                  target="_blank"
                  style="color: white;"
                ></a
              ></el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <!-- <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:app:add')"
            >添加
          </el-button>
        </template> -->
        <!-- 操作列 -->
        <!-- <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:app:edit')"
            >修改
          </el-link>
        </template> -->
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <AppEdit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import AppEdit from "./app-edit";

export default {
  name: "AccountsDivision",
  components: { AppEdit },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/Ordersub/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },
        {
          prop: "pay_order_number",
          label: "支付订单号",
          width: 200,
          align: "center",
          showOverflowTooltip: true
        },
        {
          prop: "sub_order_no",
          label: "分账单号",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "mch_id",
          label: "服务商商户号",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },

        {
          prop: "pay_amount",
          label: "支付金额",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center"
        },
        {
          prop: "scale",
          label: "分账比例",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center"
        },
        {
          prop: "sub_amount",
          label: "分账金额",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center"
        },
        {
          prop: "add_time",
          label: "添加时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: "sub_time",
          label: "请求分账时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center"
          // formatter: (row, column, cellValue) => {
          //   return this.$util.toDateString(cellValue);
          // }
        },
        {
          prop: "status",
          label: "状态",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (cellValue == 1) {
              return "未请求";
            } else if (cellValue == 2) {
              return "处理中";
            } else if (cellValue == 3) {
              return "处理完成";
            }
          }
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      process: process.env.VUE_APP_API_BASE_URL,
      token: "",
      base64EncodedToken: "",
      urlEncodedToken: ""
    };
  },
  watch: {
    token(value) {
      if (value) {
        this.base64EncodedToken = btoa(value);
        console.log(
          "token加密第一次的值为:",
          value,
          "token加密第二次的值为:",
          this.base64EncodedToken
        );
        this.urlEncodedToken = encodeURIComponent(this.base64EncodedToken);
        console.log(
          "token加密第二次的值为:",
          this.base64EncodedToken,
          "token加密第三次的值为:",
          this.urlEncodedToken
        );
      }
    }
  },
  created() {
    this.getToken();
    this.$http
      .post("/payway/getAll")
      .then(res => {
        console.log(res.data.data, "paywww");
        if (res.data.code === 0) {
          this.paywayList = res.data.data;
          console.log(res.data.data, "paywwwLLL");
        } else {
          this.$message.error(res.data.msg);
        }
      })
      .catch(e => {
        this.loading = false;
        this.$message.error(e.message);
      });
  },
  methods: {
    // 获取token
    getToken() {
      console.log("this.downLoadFilterList", this.downLoadFilterList);
      this.token = localStorage.getItem("access_token");
      console.log("token", this.token);
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 下载数据Excel */
    exportExcel() {
      console.log("this.where", this.where);
      var dtx = document.getElementById("Export");
      dtx.click();
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/app/delete", { id: row.id })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的项目吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/app/delete", { id: this.selection.map(d => d.id) })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    }
  }
};
</script>

<style scoped></style>
